/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view.GUI.collections;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.List;
import java.util.Map;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import model.graphic.objects.GNode;
import view.GUI.LabelReader;

/**
 *
 * @author Ben
 */
public class CollectionEditor extends JDialog implements ActionListener, WindowListener {
    private AbstractTableModel model;
    private JTable table;
    private GNode gn;

    private void createLayout(){
        JPanel p = new JPanel();
        this.setLocationRelativeTo(null);
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        table = new JTable(model);
        //table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(table);
        p.add(scrollPane);
        JPanel pnlButtons = new JPanel();
        JButton btnInsert = new JButton(LabelReader.getInstance().getString("Insert"));
        JButton btnDelete = new JButton(LabelReader.getInstance().getString("Delete"));
        JButton btnAdd = new JButton(LabelReader.getInstance().getString("Add"));
        pnlButtons.add(btnInsert);
        pnlButtons.add(btnDelete);
        pnlButtons.add(btnAdd);
        btnInsert.setActionCommand("Insert");
        btnDelete.setActionCommand("Delete");
        btnAdd.setActionCommand("Add");
        btnInsert.addActionListener(this);
        btnDelete.addActionListener(this);
        btnAdd.addActionListener(this);
        p.add(pnlButtons);
        this.addWindowListener(this);
        this.setContentPane(p);
        this.pack();
    }

    public void windowActivated(WindowEvent arg0) { }
    public void windowClosed(WindowEvent arg0) {  }
    public void windowDeactivated(WindowEvent arg0) { }
    public void windowDeiconified(WindowEvent arg0) { }
    public void windowIconified(WindowEvent arg0) {  }
    public void windowOpened(WindowEvent arg0) {  }

    public void windowClosing(WindowEvent arg0) {
        gn.setValue(((ICollectionTableModel)model).toString());
    }

    public static boolean isCollection(Object value){
        return List.class.isInstance(value) || Map.class.isInstance(value);
    }

    public CollectionEditor(Object obj,GNode gn) {
        this.gn = gn;
        if(List.class.isInstance(obj)){
            model = new TableListModel((List)obj);
        }else if(Map.class.isInstance(obj)){
            model = new TableMapModel((Map)obj);
        }
        createLayout();
    }

    public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals("Insert")){
            int[] rows = table.getSelectedRows();
            for(int i = rows.length - 1; i >= 0; i--){
                    ((ICollectionTableModel)model).addRow(rows[i]);
            }
            table.repaint();
        }else if(e.getActionCommand().equals("Delete")){
            int[] rows = table.getSelectedRows();
            for(int i = rows.length - 1; i >= 0; i--){
                    ((ICollectionTableModel)model).removeRow(rows[i]);
            }
            table.repaint();
        }else if(e.getActionCommand().equals("Add")){
            ((ICollectionTableModel)model).addRow(0);
            table.repaint();
        }
    }
}
